import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { Subject } from 'rxjs';
import * as greenworks from '../../assets/steam';
import { FriendFlags } from '../../assets/steam/enums';

@Injectable({
  providedIn: 'root',
})
export class GreenworksService {
  greenworks!: typeof greenworks;


  constructor() {
    try {
      this.greenworks = nw.require(`${environment.assetsPath}/steam/greenworks`);
      console.log('greenworks', this.greenworks);
      if (this.isSteamRunning()) {
        this.greenworks.init();
        this.listenSteamworksEvent();
      }
    } catch (e) {
      console.error('GreenworksService constructor error:', e);
    }
  }

  listenSteamworksEvent() {
    // this.greenworks.on(SteamEvent.LobbyCreated, (result, steamIDLobby) => {
    //   console.log('LobbyCreated', result, steamIDLobby);
    // }, e => {
    //   console.error('e', e);
    // });
    // this.greenworks.on(SteamEvent.LobbyEnter, (steamIDLobby, chatPermissions, locked, chatRoomEnterResponse) => {
    //   console.log('LobbyEnter', steamIDLobby, chatPermissions, locked, chatRoomEnterResponse);
    // }, e => {
    //   console.error('e', e);
    // });
    // this.greenworks.on(SteamEvent.LobbyDataUpdate, (steamIDLobby: string, steamIDMember: string, success: boolean) => {
    //   console.log('LobbyDataUpdate', steamIDLobby, steamIDMember, success);
    // }, e => {
    //   console.error('e', e);
    // });

  }

  activateAchievement(achievement: string) {
    let subject = new Subject<void>();
    this.greenworks.activateAchievement(achievement, () => {
      subject.next();
    }, (e: any) => {
      subject.error(e);
    });
    return subject.asObservable();
  }

  clearAchievement(achievement: string) {
    let subject = new Subject<void>();
    this.greenworks.clearAchievement(achievement, () => {
      subject.next();
    }, (e: any) => {
      subject.error(e);
    });
    return subject.asObservable();
  }

  getAchievementNames() {
    return this.greenworks.getAchievementNames();
  }


  randomBuffer() {
    let len = 64;
    let chars = 'abcdef0123456789';
    let maxPos = chars.length;
    let character = '';
    for (let i = 0; i < len; i++) {
      character += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return character;
  }

  testAuth() {
    // let that = this;
    // let str = 'ed9386073647cea58b7721490d59ed445723f0f66e7414e1533ba33cd803bdbd';//480专用
    // let greenworks = this.greenworks;
    // console.log('greenworks', greenworks);
    // greenworks.getEncryptedAppTicket('test_content', function (ticket: any) {
    //   console.log('ticket: ' + ticket.toString('hex'));
    //   const randomStr = that.randomBuffer();
    //   console.log('randomStr', randomStr);
    //   let key = Buffer.from(str, 'hex');
    //   console.log('key', key);
    //   console.log('key1', key.toString('hex'));
    //   console.log(key.length, greenworks.EncryptedAppTicketSymmetricKeyLength);
    //   let decrypted_app_ticket = greenworks.decryptAppTicket(ticket, key);
    //   console.log('decrypted_app_ticket', decrypted_app_ticket);
    //   if (decrypted_app_ticket) {
    //     console.log('isTicketForApp', greenworks.isTicketForApp(decrypted_app_ticket, greenworks.getAppId()));
    //     console.log('getTicketAppId', greenworks.getTicketAppId(decrypted_app_ticket));
    //     console.log('getTicketSteamId', greenworks.getTicketSteamId(decrypted_app_ticket));
    //     console.log('getTicketIssueTime', greenworks.getTicketIssueTime(decrypted_app_ticket));
    //   }
    // }, function (err: any) {
    //   throw err;
    // });
  }

  isSteamRunning() {
    return this.greenworks.isSteamRunning();
  }

  test() {
    //109775242143484744
    // let lobbyId = this.greenworks.createLobby(LobbyType.Public, 2);
    // console.log('lobbyId', lobbyId);
    // let result = this.greenworks.isGameOverlayEnabled();
    // console.log('test', result);
    // console.log('getFileCount', this.greenworks.getFileNameAndSize(0));
    let friends = this.greenworks.getFriends(FriendFlags.Immediate);
    console.log('friends', friends);
    for (let i = 0; i < friends.length; i++) {
      if (i !== 0) break;
      let handle = this.greenworks.getSmallFriendAvatar(friends[0].getRawSteamID());
      console.log('handle', handle);
      let imageSize = this.greenworks.getImageSize(handle);
      console.log('image_buffer', imageSize.height, imageSize.width);
      // let image_buffer = this.greenworks.getImageRGBA(handle);
      // console.log('image_buffer', image_buffer, typeof image_buffer);
    }
    // let result = this.greenworks.getLaunchCommandLine();
    // console.log('result', result);
  }

}
